/*
 *定义了一个公共对象
 * 版本：1.0
 */
define(function(require, exports, module) {
	/* 所有的ajax请求都会通过这个过滤 */
	exports.$$ = {
		ajax: function(arr) {
			yun_common.loading.open();
			
			var success = arr.success;
			var error = null;
			if (!arr.hasOwnProperty("dataType")) {
				arr.dataType = 'text';
			}

			if (arr.hasOwnProperty("error")) {
				error = arr.error;
			}

			if (!arr.hasOwnProperty("type")) {
				arr['type'] = 'post';
			}

			arr.error = function() {
				yun_common.loading.close();
				
				if (error != null) {
					error();
				} else {
					system_error_hint("系统错误 ");
				}
			};

			arr.success = function(data) {
				yun_common.loading.close();
				if (arr.dataType == 'json') {
					if (data.hasOwnProperty("yunshao_system_hint_20151109")) {
						if(!yunshao_system_hint_20151109_ctrl(data.yunshao_system_hint_20151109)){
							return ;
						};
					}

					if (data.hasOwnProperty("error")) { // 如果没有转化的json没有这个字段则不用过滤
						window.location.href = data.error.login_url;
					} else {
						success(data);
					};
				} else {
					try {
						//alert(data);
						var jsonData = eval("("+data+")"); // 转化成json
						//系统提示语
						if (jsonData.hasOwnProperty("yunshao_system_hint_20151109")){
							if(!yunshao_system_hint_20151109_ctrl(jsonData.yunshao_system_hint_20151109)){
								return ;
							};
						}

						if (jsonData.hasOwnProperty("error")) { // 如果没有转化的json没有这个字段则不用过滤
							window.location.href = jsonData.error.login_url;
						} else {
							success(data);
						};
					} catch (e) {
						success(data);
					}
				}
			};
			
			
			//判断是不是异步表单提交如果是就做表单提交，不是就直接做异步提交
			if(arr.hasOwnProperty("form")){
				arr.form.ajaxForm(arr);
			}else{
			    $.ajax(arr);
			}
		},
	};

	function yunshao_system_hint_20151109_ctrl(jsonObj) {
		var code = jsonObj.code;
		var exit = jsonObj.exit;
		//通过的提示
		if (code == '100') {
			system_success_hint(jsonObj.content);
		//警告的提示
		} else if (code == '101') {
			system_error_hint(jsonObj.content);
		//错误的提示
		}else if (code == '102') {
			system_error_hint(jsonObj.content);
		}

		//如果返回1则直接返回，将不会执行继承代码
		if (exit = 1) {
			return false;
		}
		
		return true;
	}

	/*添加成功提示*/
	function system_success_hint(hint) {
		$("body").append("<div  id='add_success_hint'  style='position: fixed; background: #2F9833; z-index: 100; left: 0px; top: 0px; border-radius: 10px; line-height: 30px; padding-left: 10px; padding-right: 10px; color: #FFFFFF; opacity: 0;'>" + hint + "</div>");
		var add_success_hint = $("#add_success_hint");
		var height = add_success_hint.height;
		var width = add_success_hint.width;
		add_success_hint.css('left', '50%');
		add_success_hint.css('top', '100px');
		add_success_hint.css('margin-left', (width / 2) * -1);
		add_success_hint.transition({
			opacity: 1,
			duration: 300,
			easing: 'ease-in-out',
			complete: function() {
				$(this).transition({
					opacity: 0,
					duration: 5000,
					easing: 'ease-in-out',
					complete: function() {
						$(this).remove();
					}
				});
			}
		});
	}

	/*添加失败提示*/
	function system_error_hint(hint) {
		/*  <div class="alert alert-success alert-block"> <a class="close" data-dismiss="alert" href="#">×</a>
              <h4 class="alert-heading">Success!</h4>
               commodo cursus magna, vel scelerisque nisl consectetur et. </div>*/
		$("body").append("<div  id='add_success_hint'  style='position: fixed; background:red ; z-index: 100; left: 0px; top: 0px; border-radius: 10px; line-height: 30px; padding-left: 10px; padding-right: 10px; color: #FFFFFF; opacity: 0;'>" + hint + "</div>");
		var add_success_hint = $("#add_success_hint");
		var height = add_success_hint.height;
		var width = add_success_hint.width;
		add_success_hint.css('left', '50%');
		add_success_hint.css('top', '100px');
		add_success_hint.css('margin-left', (width / 2) * -1);
		add_success_hint.transition({
			opacity: 1,
			duration: 300,
			easing: 'ease-in-out',
			complete: function() {
				$(this).transition({
					opacity: 0,
					duration: 4000,
					easing: 'ease-in-out',
					complete: function() {
						$(this).remove();
					}
				});
			}
		});
	}

	window.$$ = exports.$$;
});